package com.agan.aganjavaspyder.crawler.ct_demo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.SchedulerContext;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author cglovewx
 */
@PipelineName("CtPhonePipeline")
@Slf4j
public class CtPhontPipeline implements Pipeline<StartCtPhone> {

	@SneakyThrows
	@Override
	public void process(StartCtPhone startCtPhone) {

		String resultText = parseString(startCtPhone.getResultText());
		JSONObject jsonObject = JSONUtil.parseObj(resultText);
		System.out.println("jsonObject------"+jsonObject.toString());
		JSONObject dataObject = JSONUtil.parseObj(jsonObject.get("dataObject"));
		System.out.println("dataObject------"+dataObject);
		//处理页信息
		PageInfo pageInfo = JSONUtil.parseObj(dataObject.get("pageInfo")).toBean(PageInfo.class);
		int currPage = startCtPhone.getPageNum();
		int totalPage =  pageInfo.getAllPages();
		log.info("当前页数:{}",currPage);
		if(currPage > totalPage){
			log.info("页数大于，跳出处理");
			return;
		}
		JSONArray proList = JSONUtil.parseArray(dataObject.get("proList"));
		List<PhoneList> phoneLists = proList.toList(PhoneList.class);
		//单页所有手机
		for (PhoneList phone : phoneLists) {
			System.out.println(phone.getXsj() + "----" + phone.getLongTitle() + "-----" + phone);
			if(CollUtil.isNotEmpty(phone.getGroupProd())){
				//手机规格详情
				List<PhoneGroupProd> prods= phone.getGroupProd();
				for (PhoneGroupProd prod : prods){
					//入库对象
					String detilUrl = "http://www.189.cn/products/"+prod.getSalesCode()+".html?internal_search=1";
					//继续抓取详情页
					HttpRequest sub = startCtPhone.getRequest().subRequest(detilUrl);
					SchedulerContext.into(sub);
					Thread.sleep(1000);
				}

			}
		}
		//继续抓取下一页构造下一页
		//if (currPage < totalPage) {
		if (currPage < 2) {
			int nextPage = currPage + 1;
			String nextUrl = "http://www.189.cn/dqmh/tianyiMall/searchMallAction.do?method=goToSearch&searchtext="+startCtPhone.getSearchtext()+"&pageSize=16&internal_search=1&shopId=10001&currentPage="+nextPage+"&listType=0";
			HttpRequest request = startCtPhone.getRequest();
			SchedulerContext.into(request.subRequest(nextUrl));
		}

	}

	/**
	 * 处理初始化数据成可用数据
	 * @return
	 */
	private String parseString(String result){
		//解析数据
		String resultText = result;
		System.out.println("resultText1111:"+resultText);
		//正哲过滤 换行符号等
		resultText = StrUtil.removeAllLineBreaks(resultText);
		//找到resultInfoObj 之后的
		resultText = StrUtil.subAfter(resultText,"var resultInfoObj = ",false);
		//找到resultInfoObj 和;	var resultInfo = JSON.stringify(resultInfoObj); 之间的数据
		resultText = StrUtil.subBefore(resultText,";\tvar resultInfo = JSON.stringify(resultInfoObj);",false);
		System.out.println("resultText:"+resultText);
		return resultText;
	}



}